package com.biz.primus.model.order.vo.request;

import com.biz.primus.model.order.enums.*;
import com.biz.primus.model.order.vo.*;
import com.biz.primus.model.vendor.enums.PaymentWay;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.Lists;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.List;

/**
 * 订单请求模型
 *
 * @author Tiger Feng
 * @date 2017年11月09日
 */
@Data
@NoArgsConstructor
@ApiModel("订单请求模型")
public class OrderRequestVO implements Serializable {
    private static final long serialVersionUID = 833811471238195842L;

    @ApiModelProperty("订单明细，商品")
    private List<OrderItemRequestVO> items;
    @ApiModelProperty("是否是秒杀订单,true:是,false:否")
    private Boolean seckillOrder;
    @ApiModelProperty("订单类型(FLASH:闪送、STORE:商城、SECKI:秒杀、SYNCH:同步)")
    private OrderType orderType;
    @ApiModelProperty("订单IP")
    private String orderIp;
    @ApiModelProperty("订单名称")
    private String orderName;
    @ApiModelProperty("订单编号")
    private String orderCode;
    @ApiModelProperty("门店编码(门店或省仓店)")
    private String depotCode;

    //@ApiModelProperty(value = "订单商品总金额,物流费用不计算在内", hidden = true)
    //@JsonIgnore
    //private Integer orderAmount;
    @ApiModelProperty(value = "订单应付,物流费用在内", hidden = true)
    @JsonIgnore
    private Integer payableAmount;

    @ApiModelProperty(value = "商品总价，商品的最终价相加", hidden = true)
    @JsonIgnore
    private Integer productsAmount;
    @ApiModelProperty(value = "商品市场价总价", hidden = true)
    @JsonIgnore
    private Integer marketPriceTotal;
    @ApiModelProperty(value = "商品会员价总价", hidden = true)
    @JsonIgnore
    private Integer memberPriceTotal;
    @ApiModelProperty(value = "电子钱包总价", hidden = true)
    @JsonIgnore
    private Integer eWalletPriceTotal;
    @ApiModelProperty(value = "商品特价总价", hidden = true)
    @JsonIgnore
    private Integer spcialOfferPriceTotal;
    @ApiModelProperty(value = "促销抵扣总金额", hidden = true)
    @JsonIgnore
    private Integer discountPromotionMoneyTotal;
    @ApiModelProperty(value = "优惠券抵扣总金额", hidden = true)
    @JsonIgnore
    private Integer discountCouponMoneyTotal = 0;

    @ApiModelProperty(value = "订单包含的商品名称 以,,,做分隔", hidden = true)
    @JsonIgnore
    private String productNames;
    @ApiModelProperty(value = "用户备注")
    @JsonIgnore
    private String userRemark;
    @ApiModelProperty(value = "商户自备注(一般用于客服)")
    @JsonIgnore
    private String selfRemark;
    //    @ApiModelProperty(value = "物流费")
//    @JsonIgnore
//    private Integer deliveryMoney;
    @ApiModelProperty(value = "商品总数量")
    @JsonIgnore
    private Integer quantity;
    @ApiModelProperty(value = "商品箱规中 需要在前端展示瓶箱切换的数量", hidden = true)
    @JsonIgnore
    private Integer packageNumber;
    @ApiModelProperty("物流信息")
    private OrderDeliveryInfoVO delivery;

    //    @ApiModelProperty(value = "物流状态<p>\n" +
//            "     FOR_OUTBOUND：待出库、\n" +
//            "     FOR_TAKE_THEIR：待自提、\n" +
//            "     DISTRIBUTING：配送中、\n" +
//            "     SIGNED：已签收、\n" +
//            "     PART_OF_SIGNED：部分签收、\n" +
//            "     REJECT：拒收\n" +
//            "     </p>", hidden = true)
//    @JsonIgnore
//    private DeliveryState deliveryState;
    @ApiModelProperty(value = "支付状态<p>\n" +
            "     WAIT_PAY：待支付、\n" +
            "     PAY_SUCCESS：支付成功、\n" +
            "     PAY_FAILURE：支付失败、\n" +
            "     PAY_ON_DELIVERY：货到付款</p>", hidden = true)
    @JsonIgnore
    private PaymentState paymentState;
    @ApiModelProperty(value = "评价状态(WAITING_COMMENTS：待评价、ALREADY_COMMENTS：已评价)", hidden = true)
    @JsonIgnore
    private CommentState commentState;
    @ApiModelProperty(value = "订单状态(CREATED：新创建订单、\n" +
            "     PAID：已支付、\n" +
            "     DELIVERED：已发货、\n" +
            "     RECEIVED：已签收、\n" +
            "     ORDER_FINISHED：订单完成)", hidden = true)
    @JsonIgnore
    private OrderState state;
    @ApiModelProperty(value = "特殊订单类型（voice_order：语音购物订单）,hidden = true")
    @JsonIgnore
    private SpecialOrderType specialOrderType;
    @ApiModelProperty(value = "删除标志", hidden = true)
    @JsonIgnore
    private Boolean isSignDeleted;
    @ApiModelProperty(value = "是否可退货", hidden = true)
    @JsonIgnore
    private Boolean returnable;
    @ApiModelProperty(value = "是否关联了退货单", hidden = true)
    @JsonIgnore
    private Boolean haveReturned;
    @ApiModelProperty(value = "退货单ID", hidden = true)
    @JsonIgnore
    private Long orderReturnId;
    @ApiModelProperty(value = "是门店订单（true：门店订单、false：总店订单）", hidden = true)
    @JsonIgnore
    private Boolean isStoreOrder;

    @ApiModelProperty(value = "订单来源<p>\n" +
            "     ios：iOS、\n" +
            "     android：安卓、\n" +
            "     wechat：微商城、\n" +
            "     </p>")
    @JsonIgnore
    private OrderClient client;

    @ApiModelProperty(value = "当前订单汇总可支持的支付方式列表", hidden = true)
    @JsonIgnore
    private List<OrderPaymentWay> supportPaymentWays;

    /**
     * 订单请求模型中不接受"选择的促销"，在上层模型中直接同步
     */
    @ApiModelProperty(value = "促销(当前选择的促销)", hidden = true)
    @JsonIgnore
    private OrderPromotionVO promotion;
    @ApiModelProperty(value = "可选择促销", hidden = true)
    @JsonIgnore
    private List<OrderPromotionVO> optionalPromotions;

    @ApiModelProperty(value = "已选赠送商品列表")
    private List<OrderProductVO> selectedComplimentaryProducts;
    @ApiModelProperty(value = "可选择赠送商品列表", hidden = true)
    @JsonIgnore
    private List<OrderProductVO> optionalComplimentaryProducts;
    @ApiModelProperty(value = "赠送商品数量", hidden = true)
    @JsonIgnore
    private Integer complimentaryQuantity;
    @ApiModelProperty(value = "首单立减促销减免金额", hidden = true)
    @JsonIgnore
    private Integer firstOrderCutPromotionAmount;

    @ApiModelProperty(value = "可选择的配送列表", hidden = true)
    @JsonIgnore
    private List<DeliveryVO> optionalDeliveries;
    @ApiModelProperty("已选择的配送方式")
    private DeliveryVO selectedDelivery;

    @ApiModelProperty(value = "支付方式", hidden = true)
    @JsonIgnore
    private OrderPaymentWay paymentWay;


    public void refreshParameter() {
        // 订单包含的商品名称
        List<String> productNameList = Lists.newArrayList();
        // 商品总价
        productsAmount = 0;
        // 商品市场价总价
        marketPriceTotal = 0;
        // 商品会员价总价
        memberPriceTotal = 0;
        // 电子钱包总价
        eWalletPriceTotal = 0;
        // 商品特价总价
        spcialOfferPriceTotal = 0;
        // 促销总金额
        discountPromotionMoneyTotal = 0;
        // 商品数量
        quantity = 0;
        if (items == null || items.isEmpty()) {
            return;
        }
        // 用户购买的商品 统计
        for (OrderItemRequestVO itemRequestVO : items) {
            int qty = itemRequestVO.getQuantity();
            productNameList.add(itemRequestVO.getProductName());
            // 是否满足整箱
            if (quantity >= packageNumber
                    && itemRequestVO.getFclSinglePrice() != null
                    && itemRequestVO.getFclSinglePrice() > 0) {
                itemRequestVO.setFinalPrice(itemRequestVO.getFclSinglePrice());
            } else {
                itemRequestVO.setFinalPrice(itemRequestVO.getMemberPrice());
            }
            // 判断支付方式是否为电子钱包
            if (paymentWay == OrderPaymentWay.balance
                    && itemRequestVO.getEWalletPrice() != null
                    && itemRequestVO.getEWalletPrice() > 0) {
                itemRequestVO.setFinalPrice(
                        itemRequestVO.getFinalPrice() > itemRequestVO.getEWalletPrice() ?
                                itemRequestVO.getEWalletPrice() : itemRequestVO.getFinalPrice()
                );
            }
            quantity += qty;
            productsAmount += (itemRequestVO.getFinalPrice() * qty);
            marketPriceTotal += (itemRequestVO.getMarketPrice() * qty);
            memberPriceTotal += (itemRequestVO.getMemberPrice() * qty);
            eWalletPriceTotal += (itemRequestVO.getEWalletPrice() * qty);
            spcialOfferPriceTotal += (itemRequestVO.getSpecialOfferPrice() * qty);
            discountPromotionMoneyTotal += (itemRequestVO.getDiscountPromotionMoney() * qty);
        }
        // 将"首单促销减免金额" 加到 "促销总减免金额"中
        discountPromotionMoneyTotal += firstOrderCutPromotionAmount;
        productNames = StringUtils.join(productNameList.toArray(), ',');
        // 订单应付=商品总价-优惠券金额+运费-首单促销减免金额
        payableAmount = (productsAmount - discountCouponMoneyTotal + delivery.getDeliveryMoney());
    }
}
